<?php
namespace Framework;

class Middleware
{
	public $queue = [];

	public $request;

	public function __construct()
	{

	}

	public function register($middleware){
		$this->queue[] = $middleware;
	}


	function middleware($handler)
	{
	    foreach (array_reverse($this->queue) as $key => $middleware) 
	    {
	        $next = function($handler) use($middleware){
	            return function() use ($handler, $middleware) {
	                $class   = new $middleware();
	                $request = app()->request;
	                return $class->handle($request, $handler);
	            };
	        };
	        $handler=$next($handler);
	    }
	    return $handler;
	}

}